﻿using System;
using System.Collections.Generic;
using MyFloridaHouse.DataAccess;
using MyFloridaHouse.libSTO;
using MyFloridaHouse.libSvCommonUtilities;
using MyFloridaHouse.libSvDTO;

namespace MyFloridaHouse.libServiceBusinessRules
{
    public static class CensusGeometry
    {
        public static CensusGeometrySTO GetCensusGeometry(DateTime clientFileTimestamp)
        {
            var results = new CensusGeometrySTO();

            try
            {
                bool fileIsStale = true;
                try
                {
                    // check to see if the client data file is up to date
                    fileIsStale = File.IsFileStale(clientFileTimestamp, Constants.CloudBlobBaseURL, Constants.CloudBlobContainer, Constants.CloudTimestampDirectory, Constants.CloudCensusGeometryDataTimestampKey);

                }
                catch
                {
                    fileIsStale = true;
                }
                if (fileIsStale)
                {
                    var dal = new DAL_CensusData();
                    List<CensusGeometryDTO> packList = dal.GetCensusGeometry();
                    CensusGeometryListDTO dtoList = new CensusGeometryListDTO();
                    dtoList.censusGeometryList = packList;
                    byte[] serializedList = Serializer.Serialize(dtoList);
                    byte[] serializedAndCompressedList = CompressionManager.Compress(serializedList);
                    results.packedDataList = serializedAndCompressedList;
                }
            }
            catch (Exception ex)
            {
                results.errorMessage = " Can not download Census Geometry data :" + ex.Message;
            }
            return results;
        }
    }
}
